class Solution: def removeDuplicateLetters(self , str: str) -> str: # write code here rindex = {c:i for i,c in enumerate(str)} res = "" for i,c in enumerate(str): if c not in res: while c < res[-1:] and i < rindex[res[-1]]: res = res[:-1] res += c return res